package JurisPro.modelo.banco;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import JurisPro.modelo.Foro;


public class ForoDAO {
	
private static ForoDAO singleton = null;
	
	public static ForoDAO getInstance() {
		if(singleton == null)
			singleton = new ForoDAO();
		return ForoDAO.singleton;
	}
	
	
	public int insertNewForo(Foro foro) {
		String sql = "INSERT INTO Foro " +
			"(foro) VALUES (?)";
		PreparedStatement state = Conexao.getInstance().getPrepState(sql);
		int rows = 0;
		try {
			state.setString(1, foro.getForo());			
			rows = state.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}										
		return rows;		
	}
	
	
	public ArrayList<Foro> selectForoAll() {
		String sql = "SELECT IDForo, foro FROM Foro;";
		ResultSet rs = Conexao.getInstance().selectComand(sql);
	    Foro foro = null;
		ArrayList<Foro> foros = new ArrayList<Foro>();
		try {
			while (rs.next()) {
				foro = new Foro(rs.getInt("IDForo"), rs.getString("foro"));
				foros.add(foro);
			}
			rs.close();	
			
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return foros;
	}


	public int deleteForo(Foro foro) {
		String sql = "DELETE Foro WHERE foro = ?";
		PreparedStatement state = Conexao.getInstance().getPrepState(sql);
		int rows = 0;
		try {
			state.setString(1, foro.getForo());
			rows = state.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}										
		return rows;	
	}

}
